package com.yk._14Volatile;

/**
 * @author xuxing
 * @data 2020/4/20
 */

//不保证原子性
public class _02Volatile {
    private static volatile int num = 0;

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 20; i++) {
            new Thread(()->{
                for (int j = 0; j < 1000; j++) {
                    //不是原子性操作 可使用原子类解决该问题
                    num++;
                }
            }).start();
        }

        while(Thread.activeCount()>2){

        }
        System.out.println(Thread.currentThread().getName()+"  num = " + num);
    }
}
